import es
import os
from time import strftime

path = es.getAddonPath('absolutadmin') + '/logs/debug.log'

def log(text, error=0, writeTime=1):
    def wf(time, wt, method):
        f = open(path, method + 'b')
        f.write('%s%s\n' % (time + ' - ' if wt else '', text))
        f.close()
    def ef(time, text, method):
        f = open(path.replace('debug.log', 'errors.log'), method + 'b')
        f.write('%s - %s\n' % (time, text))
        f.close()
    es.dbgmsg(0, '[Absolut Admin] ' + text)
    time = strftime('%Y-%m-%d %H:%M:%S')
    try:
        wf(time, writeTime, 'a')
    except IOError:
        wf(time, writeTime, 'w')
    if error:
        try:
            ef(time, text, 'a')
        except IOError:
            ef(time, text, 'w')

def clean():
    if os.path.isfile(path):
        os.remove(path)
